package com.huike.xingxing.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Toast;

import com.android.volley.NetworkError;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.huike.xingxing.MainActivity;
import com.huike.xingxing.R;
import com.huike.xingxing.application.BaseApplicaton;

import androidx.appcompat.app.AppCompatActivity;

public class BaseActivity extends AppCompatActivity {


    private RequestQueue queue;
    private ProgressDialog show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        BaseApplicaton.getInstance().addActivity(this);
    }

    public void showLoading(){
        if(show == null){
            show = ProgressDialog.show(BaseActivity.this, "提示", "数据加载中...");
            show.setCancelable(true);
        }
        show.show();
    }
    public void hide(){
        if (show != null && show.isShowing()){
            show.dismiss();
        }
    }

    public void checkError(VolleyError error){

        if(error!=null){
            if(error instanceof TimeoutError){
                Toast.makeText(this,"网络请求超时，请重试！",Toast.LENGTH_SHORT).show();
                return;
            }
            if(error instanceof ServerError) {
                Toast.makeText(this,"服务器异常",Toast.LENGTH_SHORT).show();
                return;
            }
            if(error instanceof NetworkError) {
                Toast.makeText(this,"请检查网络",Toast.LENGTH_SHORT).show();
                return;
            }
            if(error instanceof ParseError) {
                Toast.makeText(this,"数据格式错误",Toast.LENGTH_SHORT).show();
                return;
            }

            Toast.makeText(this,error.getMessage(),Toast.LENGTH_SHORT).show();

        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
